package com.miaowang.salad.jobqueue;

import org.json.JSONObject;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.miaowang.salad.util.ActivityUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;

import de.greenrobot.event.EventBus;

@SuppressWarnings("serial")
public class LoginJob extends Job{

	private String phoneNum = "";
	private String identifyCode = "";
	private Context context;
	public LoginJob(Context context , String phoneNum , String identifyCode) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.phoneNum = phoneNum;
		this.identifyCode = identifyCode;
		this.context = context;
	}

	@Override
	public void onAdded() {
		
	}

	@Override
	protected void onCancel() {
		
	}

	@Override
	public void onRun() throws Throwable {
		String url = UrlUtils.USER_LOGIN+ "?" ;
		JSONObject json = new JSONObject();
		json.put("phone", phoneNum);
		json.put("code", identifyCode);
		json.put("device_uid", ActivityUtils.getDeviceId(context));
		json.put("device", "android");
		json.put("latitude", "");
		json.put("longitude", "");
        String result = OkHttpUtils.post(url, json.toString());
        OkHttpResponseData responseData = JSON.parseObject(result, OkHttpResponseData.class);
        EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.LOGIN,responseData));
	}

	@Override
	protected boolean shouldReRunOnThrowable(Throwable arg0) {
		EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.LOGIN));
		return false;
	}

}
